import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../views/Login";
import Home from "../views/Home";
import ListArticle from "../views/article/ListArticle";
import PublishArticle from "../views/article/PublishArticle";
import CommonUser from "../views/user/CommonUser";
import Admin from "../views/user/Admin";
import CategoryManage from "../views/category/CategoryManage";
import LogManage from "../views/log/LogManage";
import DataManage from "../views/data/DataManage";
import ArticleDetails from "../views/article/ArticleDetails";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Login',
        component: Login,
        hidden : true
    },
    {
        path: '/Home',
        name: 'Home',
        component: Home,
        hidden : true,
        children : [
            {
                path: '/ArticleDetails',
                name: 'ArticleDetails',
                component: ArticleDetails,
                meta :{
                    title : '文章详情'
                }
            }
        ]
    },
    {
        path: '/Home',
        name: '文章管理',
        component: Home,
        meta : {
            hasChild : true,
            title : '文章管理',
            icon : 'fa fa-book'
        },
        children : [
            {
                path: '/ListArticle',
                name: 'ListArticle',
                component: ListArticle,
                meta :{
                    title : '文章列表'
                }
            },
            {
                path: '/PublishArticle',
                name: 'PublishArticle',
                component: PublishArticle,
                meta :{
                    title : '文章发布'
                }
            }
        ]
    },
    {
        path: '/Home',
        name: '用户管理',
        component: Home,
        meta : {
            hasChild : true,
            title:'用户管理',
            icon : 'fa fa-user-circle-o'
        },
        children : [
            {
                path: '/CommonUser',
                name: 'CommonUser',
                component: CommonUser,
                meta :{
                    title : '普通用户管理'
                }
            },
            {
                path: '/Admin',
                name: 'Admin',
                component: Admin,
                meta :{
                    title : '操作员管理'
                }
            }
        ]
    },
    {
        path: '/Home',
        name: 'Home',
        component: Home,
        meta : {
            hasChild : false,
        },
        children : [
            {
                path : '/CategoryManage',
                name : 'CategoryManage',
                component : CategoryManage,
                meta :{
                    title : '类别管理',
                    icon : 'fa fa-reorder'
                }
            },
            {
                path : '/LogManage',
                name : 'LogManage',
                component : LogManage,
                meta :{
                    title : '日志管理',
                    icon : 'fa fa-file-text-o'
                }
            },
            {
                path : '/DataManage',
                name : 'DataManage',
                component : DataManage,
                meta :{
                    title : '数据统计',
                    icon : 'fa fa-bar-chart-o'
                }
            }
        ]
    },
]

const router = new VueRouter({
    routes
})

// 前置导航守卫
// to 去哪
// from 来自哪
// next 放行
import {Message} from 'element-ui'
router.beforeEach((to, from, next) => {
    document.body.style.background = '#2d3a4b';
    if (to.path == '/') {
        next();
    } else {
        let user = window.sessionStorage.getItem('loginer');
        if (user != null) {
            document.body.style.background = '#fff';
            next();
        } else {
            Message.warning({message: '请先登录'});
            next('/');
        }
    }
})

export default router
